package ${table.basePackage}.dto;

<#-- 自动收集需要导入的类型并分类 -->
<#assign javaImports = []>
<#assign thirdPartyImports = []>
<#assign projectImports = []>

<#list columns as c>
  <#if c.javaType?? && c.javaType != "String" && c.javaType?contains(".")>
    <#if c.javaType?starts_with("java.") || c.javaType?starts_with("javax.")>
      <#assign javaImports = javaImports + [c.javaType]>
    <#elseif c.javaType?starts_with("com.kexio.")>
      <#assign projectImports = projectImports + [c.javaType]>
    <#else>
      <#assign thirdPartyImports = thirdPartyImports + [c.javaType]>
    </#if>
  </#if>
</#list>

<#-- 去重并排序 -->
<#function uniqueSort list>
  <#assign result = []>
  <#list list?sort as item>
    <#if !result?seq_contains(item)>
      <#assign result = result + [item]>
    </#if>
  </#list>
  <#return result>
</#function>

<#assign javaImports = uniqueSort(javaImports)>
<#assign thirdPartyImports = uniqueSort(thirdPartyImports)>
<#assign projectImports = uniqueSort(projectImports)>

<#-- 按顺序输出import -->
<#list javaImports as importClass>
import ${importClass};
</#list>
<#if javaImports?size gt 0>

</#if>
<#-- 一对多模式需要导入List -->
<#if table.templateType?has_content && table.templateType == 'multi' && table.ext?? && table.ext.relations??>
import java.util.List;

</#if>
<#list thirdPartyImports as importClass>
import ${importClass};
</#list>
<#if thirdPartyImports?size gt 0>

</#if>
import com.kexio.common.page.PageQuery;
import com.kexio.common.excel.annotation.ExcelField;
import com.kexio.common.excel.annotation.ExcelSheet;
<#if projectImports?size gt 0>

</#if>
<#list projectImports as importClass>
import ${importClass};
</#list>

<#-- 函数：提取简单类名 -->
<#function getSimpleClassName fullClassName>
  <#if fullClassName?? && fullClassName?contains(".")>
    <#return fullClassName?split(".")?last>
  <#else>
    <#return fullClassName!"String">
  </#if>
</#function>

/**
 * ${table.comment!table.entityName} - 数据传输对象
 * 
 * 遵循Kexio开发规范：
 * - 继承 PageQuery，支持分页查询
 * - 不包含BaseEntity中的审计字段（tenant_id、create_by等）
 * - 不使用Lombok，手写getter/setter，提升调试友好性
 * - 用于接口参数传递、查询条件、导入导出
 * - 使用 @ExcelField 注解支持Excel导入导出
 * 
 * 对应数据表：${table.tableName!''}
 * 
 * @author ${user.name!"系统"}
 * @since ${now?string('yyyy-MM-dd HH:mm:ss')}
 */
@ExcelSheet(name = "${table.comment!table.entityName}")
public class ${table.entityName}DTO extends PageQuery {

<#list columns as c>
  <#-- 跳过BaseEntity中已有的字段 -->
  <#assign skipField = false>
  <#if c.columnName?? && (c.columnName?lower_case == "tenant_id" || c.columnName?lower_case == "create_by" || c.columnName?lower_case == "create_time" || c.columnName?lower_case == "create_date" || c.columnName?lower_case == "update_by" || c.columnName?lower_case == "update_time" || c.columnName?lower_case == "update_date" || c.columnName?lower_case == "deleted" || c.columnName?lower_case == "version")>
    <#assign skipField = true>
  </#if>
  
  <#if !skipField>
    /**
     * ${c.comment!(c.propertyName!'field')}
     */
    @ExcelField(name = "${c.comment!(c.propertyName!'field')}", order = ${c_index + 1}<#if c.propertyName?? && c.propertyName?lower_case?ends_with("time")>, dateFormat = "yyyy-MM-dd HH:mm:ss"</#if>)
    private ${getSimpleClassName(c.javaType)} <#if c.propertyName??>${c.propertyName}<#else>field</#if>;
    
  </#if>
</#list>

<#-- ==================== 一对多关联字段 ==================== -->
<#if table.templateType?has_content && table.templateType == 'multi' && table.ext?? && table.ext.relations??>
  <#list table.ext.relations as r>
    /**
     * ${r.comment!'关联数据'} - ${r.alias}
     * 关联表: ${r.table!''}
     * 外键字段: ${r.foreignKey!''}
     */
    private List<${r.dtoClass}> ${r.alias};
    
  </#list>
</#if>

<#-- 添加查询相关字段（日期/时间类型的范围查询） -->
<#assign hasDateField = false>
<#list columns as c>
  <#if c.javaType?? && (c.javaType?contains("Date") || c.javaType?contains("LocalDateTime") || c.javaType?contains("LocalDate") || c.javaType?contains("Timestamp"))>
    <#assign hasDateField = true>
    <#break>
  </#if>
</#list>
<#if hasDateField>

    // ==================== 查询字段 ====================
<#list columns as c>
  <#-- 跳过BaseEntity字段 -->
  <#assign skipField = false>
  <#if c.columnName?? && (c.columnName?lower_case == "tenant_id" || c.columnName?lower_case == "create_by" || c.columnName?lower_case == "create_time" || c.columnName?lower_case == "create_date" || c.columnName?lower_case == "update_by" || c.columnName?lower_case == "update_time" || c.columnName?lower_case == "update_date" || c.columnName?lower_case == "deleted" || c.columnName?lower_case == "version")>
    <#assign skipField = true>
  </#if>
  
  <#if !skipField && c.javaType?? && (c.javaType?contains("Date") || c.javaType?contains("LocalDateTime") || c.javaType?contains("LocalDate") || c.javaType?contains("Timestamp"))>
    
    /**
     * ${c.comment!(c.propertyName!'field')} - 开始时间
     */
    private ${getSimpleClassName(c.javaType)} ${c.propertyName}Start;
    
    /**
     * ${c.comment!(c.propertyName!'field')} - 结束时间
     */
    private ${getSimpleClassName(c.javaType)} ${c.propertyName}End;
  </#if>
</#list>
</#if>

<#-- getters/setters -->
<#list columns as c>
  <#-- 跳过BaseEntity中已有的字段 -->
  <#assign skipField = false>
  <#if c.columnName?? && (c.columnName?lower_case == "tenant_id" || c.columnName?lower_case == "create_by" || c.columnName?lower_case == "create_time" || c.columnName?lower_case == "create_date" || c.columnName?lower_case == "update_by" || c.columnName?lower_case == "update_time" || c.columnName?lower_case == "update_date" || c.columnName?lower_case == "deleted" || c.columnName?lower_case == "version")>
    <#assign skipField = true>
  </#if>
  
  <#if !skipField>
    
    /**
     * 获取${c.comment!(c.propertyName!'field')}
     *
     * @return ${c.comment!(c.propertyName!'field')}
     */
    public ${getSimpleClassName(c.javaType)} get<#if c.propertyName??>${c.propertyName?cap_first}<#else>Field</#if>() { 
        return this.<#if c.propertyName??>${c.propertyName}<#else>field</#if>; 
    }

    /**
     * 设置${c.comment!(c.propertyName!'field')}
     *
     * @param <#if c.propertyName??>${c.propertyName}<#else>field</#if> ${c.comment!(c.propertyName!'field')}
     */
    public void set<#if c.propertyName??>${c.propertyName?cap_first}<#else>Field</#if>(${getSimpleClassName(c.javaType)} <#if c.propertyName??>${c.propertyName}<#else>field</#if>) { 
        this.<#if c.propertyName??>${c.propertyName}<#else>field</#if> = <#if c.propertyName??>${c.propertyName}<#else>field</#if>; 
    }
  </#if>
</#list>

<#-- ==================== 一对多关联字段的getter/setter ==================== -->
<#if table.templateType?has_content && table.templateType == 'multi' && table.ext?? && table.ext.relations??>
  <#list table.ext.relations as r>
    
    /**
     * 获取${r.comment!'关联数据'} - ${r.alias}
     *
     * @return ${r.alias}
     */
    public List<${r.dtoClass}> get${r.alias?cap_first}() {
        return this.${r.alias};
    }
    
    /**
     * 设置${r.comment!'关联数据'} - ${r.alias}
     *
     * @param ${r.alias} ${r.comment!'关联数据'}
     */
    public void set${r.alias?cap_first}(List<${r.dtoClass}> ${r.alias}) {
        this.${r.alias} = ${r.alias};
    }
  </#list>
</#if>

<#-- 查询字段的getter/setter -->
<#if hasDateField>
<#list columns as c>
  <#-- 跳过BaseEntity字段 -->
  <#assign skipField = false>
  <#if c.columnName?? && (c.columnName?lower_case == "tenant_id" || c.columnName?lower_case == "create_by" || c.columnName?lower_case == "create_time" || c.columnName?lower_case == "create_date" || c.columnName?lower_case == "update_by" || c.columnName?lower_case == "update_time" || c.columnName?lower_case == "update_date" || c.columnName?lower_case == "deleted" || c.columnName?lower_case == "version")>
    <#assign skipField = true>
  </#if>
  
  <#if !skipField && c.javaType?? && (c.javaType?contains("Date") || c.javaType?contains("LocalDateTime") || c.javaType?contains("LocalDate") || c.javaType?contains("Timestamp"))>
    
    public ${getSimpleClassName(c.javaType)} get${c.propertyName?cap_first}Start() {
        return this.${c.propertyName}Start;
    }
    
    public void set${c.propertyName?cap_first}Start(${getSimpleClassName(c.javaType)} ${c.propertyName}Start) {
        this.${c.propertyName}Start = ${c.propertyName}Start;
    }
    
    public ${getSimpleClassName(c.javaType)} get${c.propertyName?cap_first}End() {
        return this.${c.propertyName}End;
    }
    
    public void set${c.propertyName?cap_first}End(${getSimpleClassName(c.javaType)} ${c.propertyName}End) {
        this.${c.propertyName}End = ${c.propertyName}End;
    }
  </#if>
</#list>
</#if>
}
